"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const koa_router_1 = __importDefault(require("koa-router"));
const RSATool_1 = __importDefault(require("../lib/RSATool"));
const configTool_1 = __importDefault(require("../com/configTool"));
const getServerInfo_1 = __importDefault(require("../lib/System/getServerInfo"));
const Authentication_1 = __importDefault(require("../auth/Authentication"));
const AuthorizeRoutes = new koa_router_1.default({ prefix: '/AuthorizeServer' });
AuthorizeRoutes
    .get('/login', async (ctx) => {
    let userName = ctx.request.query?.userName;
    let pwd = ctx.request.query?.pwd || ctx.request.query?.password;
    const userInfo = configTool_1.default.baseConfig.userList.find((item) => item.userName === userName && item.password === pwd);
    ctx.set('content-type', 'application/json;charset=utf-8');
    if (userInfo) {
        const serverInfo = await (0, getServerInfo_1.default)();
        const userCopy = { ...userInfo };
        delete userCopy.password;
        userCopy.ip = ctx.request.ip;
        userCopy.token = RSATool_1.default.createJWT(ctx.request.ip, configTool_1.default.baseConfig.privateDer);
        ctx.body = JSON.stringify({
            code: 200,
            message: '登录成功',
            data: {
                serverInfo,
                userInfo: userCopy,
                license: await Authentication_1.default.Verification()
            }
        });
        ctx.status = 200;
    }
    else {
        ctx.body = JSON.stringify({
            code: 401,
            message: '登录失败，用户名或密码错误',
            data: null
        });
        ctx.status = 401;
    }
});
exports.default = AuthorizeRoutes;
